#ifndef MOVIE_H
#define MOVIE_H

#include <QString>
#include <QVector>
#include <QSharedPointer>
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QCryptographicHash>
#include <QByteArray>
#include <QFileInfo>

#include "defines.h"

class Movie : public QObject
{
    Q_OBJECT
public:
    explicit Movie(int id = 0, QObject *parent = 0);

    ///
    /// @param  title   The title of the movie
    ///
    /// Sets the title of the movie
    inline void setTitle(const QString &title) { title_ = title; }

    ///
    /// @param  path    The file path of the movie
    ///
    /// Sets the file path of the movie
    inline void setFilePath(const QString &path) { filePath_ = path; }

    ///
    /// @param  year    The year of the movie
    ///
    /// Sets the year of the movie
    inline void setYear(const int &year) { year_ = year; }

    ///
    /// @param  cover   A byte array of the cover
    /// @param  scale   True scals the image
    ///
    /// Sets the cover of the movie. Can scale the cover if needed,
    /// otherwise the cover will only be set.
    void setCover(const QByteArray &cover, bool scale = true);

    ///
    /// @param  id      The internal database id of the image
    ///
    /// Sets the image id for the movie
    inline void setImageId(const int &id) { image_id_ = id; }

    ///
    /// @param  id      The TMDB id of the movie
    ///
    /// Sets the TMDB movie id
    inline void setTmdbId(const int &id) { tmdb_id_ = id; }

    ///
    /// @return     The title of the movie
    ///
    /// Returns the title of the movie
    inline QString getTitle() const { return title_; }

    ///
    /// @return     The path of the movie
    ///
    /// Returns the path of the movie
    inline QString getFilePath() const { return filePath_; }

    ///
    /// @return     The movie checksum
    ///
    /// Returns the movie checksum
    QString getChecksum();

    ///
    /// @return     The internal database id
    ///
    /// Returns the internal database id
    inline QVariant getId() const { return QVariant(id_); }

    ///
    /// @return     The TMDB movie id
    ///
    /// Returns the TMDB movie id
    inline int  getTmdbId() const {return tmdb_id_; }

    ///
    /// @return     The year of the movie
    ///
    /// Returns the year of the movie
    inline int getYear() const { return year_; }

    ///
    /// @return     A const reference to the cover
    ///
    /// Returns the movie cover as a const byte array reference
    inline const QByteArray &getCover() const { return cover_; }

    ///
    /// @return     True if the save process successfully
    ///
    /// Saves the movie to the database
    bool saveToDatabase();

private:
    ///
    /// Generates a movie checksum from title or filename if exists
    void createChecksum();

    ///
    /// @param  lastInsertId    A pointer to the lastInsertId variable
    ///
    /// @return     True if the saving process successfully complete
    ///
    /// Saves the cover to the database
    bool saveCover(int *lastInsertId);

    ///
    /// @return     True if the cover updated successfully
    ///
    /// Update the cover in the database and the movie table
    bool updateCover();

    QString title_;         ///< Holds the title of the movie
    QString filePath_;      ///< Holds the file path of the movie
    int year_;              ///< Holds the year of the movie
    QString checksum_;      ///< Movie checksum
    QByteArray cover_;      ///< Holds the cover of the movie
    int tmdb_id_;           ///< Holds the TMDB id
    int id_;                ///< Holds the internal database id
    int image_id_;          ///< Holds the image id for the movie cover
};

typedef QVector<QSharedPointer<Movie> > MovieList;

#endif // MOVIE_H
